
/**
 * Copyright 2014-2020  the original author or authors.
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
 * in compliance with the License. You may obtain a copy of the License at
 * <p>
 * http://www.apache.Host/licenses/LICENSE-2.0
 * <p>
 * Unless required by applicable law or agreed to in writing, software distributed under the License
 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
 * or implied. See the License for the specific language governing permissions and limitations under
 * the License.
 */
package com.upb.webserver.host;

import com.upb.webserver.account.AccountService;
import com.upb.webserver.common.pojo.account.AccountCommon;
import com.upb.webserver.common.pojo.base.BasePageResponse;
import com.upb.webserver.common.pojo.base.BaseResponse;
import com.upb.webserver.common.pojo.host.ReqAddHostVO;
import com.upb.webserver.common.pojo.host.ReqUpdateHostVO;
import com.upb.webserver.common.pojo.host.RspHostVO;
import com.upb.webserver.common.returncode.ConstantCode;
import com.upb.webserver.common.tools.JsonUtils;
import com.upb.webserver.dao.entity.TbHost;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.List;

@Log4j2
@RestController
@RequestMapping(value = "host")
public class HostController {

    @Autowired
    private AccountService accountService;
    @Autowired
    private HostManager hostManager;
    @Autowired
    private HostService hostService;


    /**
     * add new Host.
     *
     * @param param
     * @param result
     * @return
     */
    @PostMapping
    public BaseResponse addHost(@RequestBody @Valid ReqAddHostVO param, BindingResult result, HttpServletRequest request) {
        log.info("start exec method [addHost]. param:{}", JsonUtils.objToString(param));
        AccountCommon accountCommon = accountService.getAccountCommon(request);
        TbHost hostRsp =hostService.addHost(accountCommon.getPlatform(), accountCommon.getId(), param);
        BaseResponse baseResponse = new BaseResponse(ConstantCode.SUCCESS);
        baseResponse.setData(hostRsp);
        log.info("success exec method [addHost].baseResponse:{}",JsonUtils.objToString(baseResponse));
        return baseResponse;
    }


    /**
     * update Host by id.
     *
     * @param param
     * @param result
     * @return
     */
    @PatchMapping
    public BaseResponse updateHost(@RequestBody @Valid ReqUpdateHostVO param, BindingResult result) {
        log.info("start exec method [updateHost]. param:{}", JsonUtils.objToString(param));
        hostService.updateHost(param);
        log.info("success exec method [updateHost].");
        return new BaseResponse(ConstantCode.SUCCESS);
    }

    /**
     * query by page.
     *
     * @param pageNumber
     * @param pageSize
     * @return
     */
    @GetMapping(value = "/list")
    public BasePageResponse queryHostByPage(@RequestParam("pageNumber") Integer pageNumber,
                                            @RequestParam("pageSize") Integer pageSize,
                                            @RequestParam(value = "orgIdArr", required = false, defaultValue = "") List<Integer> orgIdArr,
                                            HttpServletRequest request) {
        log.info("start exec method [queryHostByPage]. pageNumber:{} pageSize:{} orgIdArr:{}", pageNumber, pageSize, JsonUtils.objToString(orgIdArr));
        AccountCommon accountCommon = accountService.getAccountCommon(request);
        BasePageResponse response = hostService.queryHostByPage(accountCommon.getPlatform(), accountCommon.getRoleName(), accountCommon.getId(), pageNumber, pageSize, orgIdArr);
        log.info("success exec method [queryHostByPage]. result:{}", JsonUtils.objToString(response));
        return response;
    }


    /**
     * @param hostId
     * @return
     */
    @GetMapping(value = "/{hostId}/detail")
    public BaseResponse queryHostDetail(@PathVariable("hostId") Integer hostId) {
        log.info("start exec method [queryHostDetail]. hostId:{}", hostId);
        RspHostVO hostVO = hostManager.queryHostDetail(hostId);
        BaseResponse baseResponse = new BaseResponse(ConstantCode.SUCCESS, hostVO);
        log.info("success exec method [queryHostDetail]. result:{}", JsonUtils.objToString(baseResponse));
        return baseResponse;
    }

    /**
     * @param hostId
     * @return
     */
    @DeleteMapping(value = "/{hostId}")
    public BaseResponse deleteHost(@PathVariable("hostId") Integer hostId) {
        log.info("start exec method [deleteHost]. hostId:{}", hostId);
        hostService.deleteByHostId(hostId);
        log.info("success exec method [deleteHost]");
        return new BaseResponse(ConstantCode.SUCCESS);
    }

//    /**
//     * @param param
//     * @param result
//     * @return
//     */
//    @PatchMapping(value = "/check-environment")
//    public BaseResponse checkHostListEnvironment(@RequestBody @Valid ReqCheckHostEnvironmentVO param, BindingResult result) {
//        log.info("start exec method [checkHostListEnvironment]. param:{}", JsonUtils.objToString(param));
//        String checkResult = hostManager.checkHostListEnvironment(param.getHostIdList());
//        BaseResponse baseResponse = new BaseResponse(ConstantCode.SUCCESS, checkResult);
//        log.info("success exec method [checkHostListEnvironment]. result:{}", JsonUtils.objToString(baseResponse));
//        return baseResponse;
//    }

}
